<template>
  <div class="article-list">
    <van-pull-refresh
      v-model="isRefreshLoading"
      @refresh="onRefresh"
      :success-text="refreshSuccessText"
      :success-duration="1500"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        @load="onLoad"
      >
        <!-- <van-cell v-for="item in list" :key="item.id" :title="item.title" /> -->
        <ArticleItem
          :article="item"
          v-for="item in list"
          :key="item.id"
        ></ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArticles } from "@/api/article.js";
import ArticleItem from "@/components/article-item";

export default {
  name: "ArticleList",
  props: {
    channel: {
      type: Object, //channel的数据类型
      required: true, //代表必传项;
    },
  },
  components: {
    ArticleItem,
  },
  data() {
    return {
      list: [], // 数据列表
      loading: false, //控制加载中 加载状态  是否在加载中;
      finished: false, // 代表 是否加载结束;
      timestamp: null, // 请求获取下一页数据的时间戳;
      error: false, //控制失败的提示状态;
      isRefreshLoading: false, //下拉刷新的状态
      refreshSuccessText: "刷新成功!", //下拉刷新成功提示
    };
  },
  methods: {
    async onLoad() {
      // 1.异步更新数据
      try {
        const {
          data: {
            data: { results },
          },
        } = await getArticles({
          channel_id: this.channel.id,
          timestamp: this.timestamp || new Date().getTime(),
          with_top: 1,
        });

        // if (Math.random() > 0.5) {
        //   JSON.parse("a,sdba");
        // }
        //2.请求结束把数据放在list中
        // this.list = results;//请求成功 把数据放在这个数组中;
        this.list.push(...results); //把这个数据添加到数组中;

        this.loading = false; // 当请求结束 则 加载结束 false代表加载结束;

        // 4.如果还有数据则加载
        if (results.length) {
          console.log("还要加载==!");
          this.timestamp = results.pre_timestamp; // 更新事件戳 便于请求下一页的数据;
        } else {
          //如果没有数据 则 加载结束
          this.finished = true;
        }
      } catch (err) {
        //   ===============================================================
        this.error = true; //失败了 当请求失败的时候 点击 再次加载
        this.loading = false; //关闭请求状态;
        console.log(err);
        // =============================================================
      }
      // 3.加载状态结束
      this.loading = false;
    },
    async onRefresh() {
      console.log(1)
      //下拉刷新后执行的函数;
      try {
        const {
          data: {
            data: { results },
          },
        } = await getArticles({
          channel_id: this.channel.id,
          timestamp: new Date().getTime(),
          with_top: 1,
        });
        this.list.unshift(...results);
        this.isRefreshLoading = false; //关闭下拉刷新状态;
        this.refreshSuccessText = `下拉刷新成功,更新了${results.length}条数据!`;
      } catch (err) {
        // console.log("错误")
        this.refreshSuccessText = "是刷新失败!";
        this.isRefreshLoading = false; //加载结束;
      }
    },
  },
};
</script>

<style scoped lang="less">
.article-list {
  height: 79vh;
  overflow-y: auto;
}
</style>
